package com.renyi.action;

import java.io.ObjectOutputStream.PutField;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.DoctorAppInfo;
import com.renyi.model.MybatisService;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class PatientRefundmentController extends BaseController {
	@Autowired
	private MybatisService mybatisService;
	// 患者-获取银行卡信息
		@RequestMapping("/getPatientBankCard")
		public void getPatientBankCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// 返回结果
			HttpResult result = new HttpResult();
			try {
				ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

				if (paramBean == null) {
					result.setCode(1003); //
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> map = paramBean.getParam();
				// 用户ID
				String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

				if ("".equals(uid)) {
					result.setCode(-1); // 0：成功
					result.setMsg("用户编号参数为空");
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> pMap = new HashMap<String, Object>();
				pMap.put("uid", uid);

				List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getPatientBankCardSQL", pMap);

				Map<String, Object> dataMap = new HashMap<String, Object>();
				Map<String, Object> mapId = null;
				if (data != null && data.size() > 0) {
					mapId = data.get(0);
					result.setData(mapId);
				}

				result.setCode(0);
				result.setMsg("");
				this.ajaxResultJson(response, result);

			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(-1);
				result.setMsg(e.getMessage());
				;// //错误消息
				this.ajaxResultJson(response, result);
			}
		}

		// 患者-更新银行卡信息
		@RequestMapping("/updatePatientBankCard")
		public void updateBankCard(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// 返回结果
			HttpResult result = new HttpResult();
			try {
				ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

				if (paramBean == null) {
					result.setCode(1003); //
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> paramMap = paramBean.getParam();
				// 用户ID
				String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
				String name = paramMap.get("name") != null ? paramMap.get("name").toString() : "";
				String idCard = paramMap.get("idCard") != null ? paramMap.get("idCard").toString() : "";
				String bankCard = paramMap.get("bankCard") != null ? paramMap.get("bankCard").toString() : "";
				String bank = paramMap.get("bank") != null ? paramMap.get("bank").toString() : "";

				if ("".equals(bank)) {
					result.setCode(-1); // 0：成功
					result.setMsg("用户编号户参数为空");
					this.ajaxResultJson(response, result);
					return;
				}

				Map<String, Object> pMap = new HashMap<String, Object>();
				pMap.put("uid", uid);
				pMap.put("name", name);
				pMap.put("idCard", idCard);
				pMap.put("bankCard", bankCard);
				pMap.put("bank", bank);
				int key = 0;
				long isExist = this.mybatisService.findIbatisListCount("getPatientBankCardIDSQL", pMap);
				if (isExist < 1) {
					key = (Integer) this.mybatisService.insertIbatisObject("insertPatientBankCardSQL", pMap);
				} else {
					key = (Integer) this.mybatisService.updateIbatisObject("updatePatientBankCardSQL", pMap);

				}
				if (key > 0) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("");
				}
				this.ajaxResultJson(response, result);

			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(-1);
				result.setMsg(e.getMessage());
				this.ajaxResultJson(response, result);
			}
		}

		// 患者-获取结算信息
		@RequestMapping("/getPatientRefundment")
		public void getPatientRefundment(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// 返回结果
			HttpResult result = new HttpResult();
			try {
				ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

				if (paramBean == null) {
					result.setCode(1003); //
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> map = paramBean.getParam();
				// 用户ID
				String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

				if ("".equals(uid)) {
					result.setCode(-1); // 0：成功
					result.setMsg("用户编号参数为空");
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> pMap = new HashMap<String, Object>();
				pMap.put("uid", uid);

				List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getPatientRefundmentSQL", pMap);

				result.setCode(0);
				result.setData(data);
				this.ajaxResultJson(response, result);

			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(-1);
				result.setMsg(e.getMessage());
				;// //错误消息
				this.ajaxResultJson(response, result);
			}
		}

	// 患者-更新结算信息
		@RequestMapping("/submitPatientRefundment")
		public void submitPatientRefundment(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// 返回结果
			HttpResult result = new HttpResult();
			try {
				ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

				if (paramBean == null) {
					result.setCode(1003); //
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> paramMap = paramBean.getParam();
				// 用户ID
				String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
				String money = paramMap.get("money") != null ? paramMap.get("money").toString() : "";

				if ("".equals(uid)) {
					result.setCode(-1); // 0：成功
					result.setMsg("用户编号参数为空");
					this.ajaxResultJson(response, result);
					return;
				}

				Map<String, Object> pMap = new HashMap<String, Object>();
				pMap.put("uid", uid);
				pMap.put("money", money);
				int key = 0;
				long isExist = this.mybatisService.findIbatisListCount("getPatientRefundmentIDSQL", pMap);
				if (isExist < 1) {
					key = (Integer) this.mybatisService.insertIbatisObject("submitPatientRefundmentSQL", pMap);
					if (key > 0) {
						result.setCode(0);
						result.setMsg(""); 
					} else {
						result.setCode(-1);
						result.setMsg("");
					}
				} else {
					result.setCode(1);
					result.setMsg("还有结算未完成");
				}

				this.ajaxResultJson(response, result);

			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(-1);
				result.setMsg(e.getMessage());
				this.ajaxResultJson(response, result);
			}
		}


}
